$(function(){
    var timeout = 30000;//Save Time Interval
    var timer;

    var timeout2 = 5000;//Load Suggention Time Interval
    var timer2;


    var first = true;
    var first2 = true;

    $(document).ready(function() {
        timer = $.timer(timeout, function() {
            if(first){
                first = false;
            }else{
                $("#frmConsultData").submit();
            }
        });

        timer2 = $.timer(timeout2, function() {
                loadSuggestionManagement();
        });     
    });

    function loadSuggestionManagement(){
        $.ajax({
            type: "POST",
            url: "../ajax/loadManagementData",
            data: {
                p_id:$("input#p_id").val(),
                c_id:$("input#c_id").val()
            },
            dataType: "json",
            success: function(data) {
                
                $("#sm_NIVTC").html(data['sm_NIVTC']);
                $("#sm_IVTStartTime").html(data['sm_IVTStartTime']);
                $("#sm_OtherTxt").html(data['sm_OtherTxt']);

                if(data["sm_IVTYn"]=="true"){
                            $('input[name=sm_IVTYn]').attr('checked', true);
                            $('input[name=sm_IVTYn]').css('background-color','green');
                }else{
                            $('input[name=sm_IVTYn').attr('checked', false);
                            $('input[name=sm_IVTYn').css('background-color','red');
                }
                if(data["sm_NIVTYn"]=="true"){
                            $('input[name=sm_NIVTYn]').attr('checked', true);
                            $('input[name=sm_NIVTYn]').css('background-color','green');
                }else{
                            $('input[name=sm_NIVTYn').attr('checked', false);
                            $('input[name=sm_NIVTYn').css('background-color','red');
                }
                if(data["sm_OtherYN"]=="true"){
                            $('input[name=sm_OtherYN]').attr('checked', true);
                            $('input[name=sm_OtherYN]').css('background-color','green');
                }else{
                            $('input[name=sm_OtherYN').attr('checked', false);
                            $('input[name=sm_OtherYN').css('background-color','red');
                }
            }
        });
    }

    $("button, input:submit").button();

    $("#frmConsultData").submit(function(){        
        $("#result").html("<img src='../assets/images/ajax-loader.gif'><font color='white'><b>กำลังบันทึก</b></font>");
        $.ajax({
            type: "POST",
            url: "../patient/save/",
            data: {
                method:$("input#method").val(),
                p_id:$("input#p_id").val(),

                //NIHSS Data
                no1a:$("select#no1a").val(),
                no1b:$("select#no1b").val(),
                no1c:$("select#no1c").val(),
                no2:$("select#no2").val(),
                no3:$("select#no3").val(),
                no4:$("select#no4").val(),
                no5a:$("select#no5a").val(),
                no5b:$("select#no5b").val(),
                no6a:$("select#no6a").val(),
                no6b:$("select#no6b").val(),
                no7:$("select#no7").val(),
                no8:$("select#no8").val(),
                no9:$("select#no9").val(),
                no10:$("select#no10").val(),
                no11:$("select#no11").val(),

                //Stroke Data
                strokeTimeYN:$("input[name=strokeTimeYN]:checked").val(),
                strokeTimeHour:$("#strokeTimeHour").val(),
                strokeTimeMinute:$("#strokeTimeMinute").val(),
                strokeTimeDMY:$("#strokeTimeDMY").val(),
                strokeNormalTimeYN:$("input[name=strokeNormalTimeYN]:checked").val(),
                strokeNormalTimeHour:$("#strokeNormalTimeHour").val(),
                strokeNormalTimeMinute:$("#strokeNormalTimeMinute").val(),
                strokeNormalTimeDMY:$("#strokeNormalTimeDMY").val(),

                
                cc:$("input#cc").val(),
                PI:$("textarea#PI").val(),
                
                //Remove
                //headache:$("input#headache").val(),
                //sideheadache:$("input#sideheadache").val(),
                //muchheadache:$("input#muchheadache").val(),
                //areaheadache:$("input#areaheadache").val(),
                //areaheadache_other:$("input#areaheadache_other").val(),

                ph_ht_yn:$("input#ph_ht_yn").is(':checked'),
                ph_ht_yrs:$("input#ph_ht_yrs").val(),
                ph_ht_med:$("input#ph_ht_med").val(),
                ph_dm_yn:$("input#ph_dm_yn").is(':checked'),
                ph_dm_yrs:$("input#ph_dm_yrs").val(),
                ph_dm_med:$("input#ph_dm_med").val(),
                ph_dyslipidemia_yn:$("input#ph_dyslipidemia_yn").is(':checked'),
                ph_dyslipidemia_yrs:$("input#ph_dyslipidemia_yrs").val(),
                ph_dyslipidemia_med:$("input#ph_dyslipidemia_med").val(),
                ph_smoking_yn:$("input#ph_smoking_yn").is(':checked'),
                ph_smoking_pack:$("input#ph_smoking_pack").val(),
                ph_smoking_yrs:$("input#ph_smoking_yrs").val(),
                ph_smoking_qyrs:$("input#ph_smoking_qyrs").val(),
                ph_heart_disease_yn:$("input#ph_heart_disease_yn").is(':checked'),
                ph_heart_disease_type:$("input#ph_heart_disease_type").val(),
                ph_heart_disease_med:$("input#ph_heart_disease_med").val(),
                ph_others_yn:$("input#ph_others_yn").is(':checked'),
                ph_others:$("input#ph_others").val(),

                //Add new
                ph_stroke_yn:$("input#ph_stroke_yn").is(':checked'),
                ph_stroke_year:$("input#ph_stroke_year").val(),
                ph_stroke_type:$("input[name=ph_stroke_type]:checked").val(),

                //Laboratory Data
                CTscanYN:$("input#CTscanYN").is(':checked'),                
                CTNoICHYN:$("input#CTNoICHYN").is(':checked'),
                CTnobrainYN:$("input#CTnobrainYN").is(':checked'),
                CTPerfusionYN:$("input#CTPerfusionYN").is(':checked'),
                MRIYN:$("input#MRIYN").is(':checked'),
                MRIResult:$("input#MRIResult").val(),
                MRINoICHYN:$("input#MRINoICHYN").is(':checked'),
                MRInobrainYN:$("input#MRInobrainYN").is(':checked'),
                MRIDWI30YN:$("input#MRIDWI30YN").is(':checked'),
                MRIDWIPWIYN:$("input#MRIDWIPWIYN").is(':checked'),
                MRAYN:$("input#MRAYN").is(':checked'),
                MRAMCAYN:$("input#MRAMCAYN").is(':checked'),
                MRAMCAso:$("input[name=MRAMCAso]:checked").val(),
                MRAACAYN:$("input#MRAACAYN").is(':checked'),
                MRAACAso:$("input[name=MRAACAso]:checked").val(),
                MRABAYN:$("input#MRABAYN").is(':checked'),
                MRABAso:$("input[name=MRABAso]:checked").val(),
                MRAICAYN:$("input#MRAICAYN").is(':checked'),
                MRAICAso:$("input[name=MRAICAso]:checked").val(),
                MRAotherYN:$("input#MRAotherYN").is(':checked'),
                MRAother:$("input#MRAother").val(),
                BloodPG:$("input#BloodPG").val(),
                BloodPT:$("input#BloodPT").val(),
                BloodPTT:$("input#BloodPTT").val(),
                BloodPLT:$("input#BloodPLT").val(),
                BloodPGYN:$("input#BloodPGYN").is(':checked'),
                BloodINRYN:$("input#BloodINRYN").is(':checked'),                
                BloodPLTYN:$("input#BloodPLTYN").is(':checked'),

                //Stroke Phy Data
                BPmin:$("input#BPmin").val(),
                BPmax:$("input#BPmax").val(),
                PR:$("input#PR").val(),
                PRminYN:$("input[name=PRminYN]:checked").val(),
                CarotidLt:$("input[name=CarotidLt]:checked").val(),
                CarotidRt:$("input[name=CarotidRt]:checked").val(),
                Heart:$("input#Heart").val(),
                HeartNA:$("input[name=HeartNA]:checked").val(),
                ConsciousnessE:$("input#ConsciousnessE").val(),
                ConsciousnessM:$("input#ConsciousnessM").val(),
                ConsciousnessV:$("input#ConsciousnessV").val(),
                OrientationTime:$("input#OrientationTime").val(),
                OrientationPlace:$("input#OrientationPlace").val(),
                OrientationPerson:$("input#OrientationPerson").val(),
                Speech:$("input[name=Speech]:checked").val(),
                Aphasia:$("input[name=Aphasia]:checked").val(),
                Fluency:$("input[name=Fluency]:checked").val(),
                Comprehension:$("input[name=Comprehension]:checked").val(),
                Paraphasia:$("input[name=Paraphasia]:checked").val(),
                Repetition:$("input[name=Repetition]:checked").val(),
                Naming:$("input[name=Naming]:checked").val(),
                CranialL:$("input#CranialL").val(),
                CranialR:$("input#CranialR").val(),
                Horner:$("input[name=Horner]:checked").val(),
                HornerLR:$("input[name=HornerLR]:checked").val(),                
                PupilsLt:$("input#PupilsLt").val(),
                PupilsLtc:$("input[name=PupilsLtc]:checked").val(),
                PupilsRt:$("input#PupilsRt").val(),
                PupilsRtc:$("input[name=PupilsRtc]:checked").val(),
                VALt:$("input#VALt").val(),
                VARt:$("input#VARt").val(),
                VFLt:$("input#VFLt").val(),
                VFRt:$("input#VFRt").val(),
                FacialWeaknessLR:$("input[name=FacialWeaknessLR]:checked").val(),
                FacialWeaknessUL:$("input[name=FacialWeaknessUL]:checked").val(),
                TongueDeviationYN:$("input[name=TongueDeviationYN]:checked").val(),
                TongueDeviationLR:$("input[name=TongueDeviationLR]:checked").val(),
                GagReflexL:$("input#GagReflexL").val(),
                GagReflexR:$("input#GagReflexR").val(),
                UvalaMD:$("input[name=UvalaMD]:checked").val(),
                UvalaLR:$("input[name=UvalaLR]:checked").val(),
                MotorLTp:$("input#MotorLTp").val(),
                MotorLTd:$("input#MotorLTd").val(),
                MotorLBp:$("input#MotorLBp").val(),
                MotorLBd:$("input#MotorLBd").val(),
                MotorRTp:$("input#MotorRTp").val(),
                MotorRTd:$("input#MotorRTd").val(),
                MotorRBp:$("input#MotorRBp").val(),
                MotorRBd:$("input#MotorRBd").val(),
                PronatorDriftingLR:$("input[name=PronatorDriftingLR]:checked").val(),
                CerebellarYN:$("input[name=CerebellarYN]:checked").val(),
                CerebellarGLR:$("input[name=CerebellarGLR]:checked").val(),
                CerebellarTYN:$("input[name=CerebellarTYN]:checked").val(),
                CerebellarFYN:$("input[name=CerebellarFYN]:checked").val(),
                SensoryTMD:$("input[name=SensoryTMD]:checked").val(),
                SensoryHlr:$("input[name=SensoryHlr]:checked").val(),
                SensoryPlr:$("input[name=SensoryPlr]:checked").val(),
                SensoryP:$("input#SensoryP").val(),
                NeglectLt:$("input#[name=NeglectLt]:checked").val(),
                NeglectRt:$("input#[name=NeglectRt]:checked").val(),
                ProprioceptionLt:$("input[name=ProprioceptionLt]:checked").val(),
                ProprioceptionRt:$("input[name=ProprioceptionRt]:checked").val(),
                FinalDXHI:$("input[name=FinalDXHI]:checked").val(),
                FinalDXHType:$("input#FinalDXHType").val(),
                VesselAP:$("input[name=VesselAP]:checked").val(),
                VesselAc:$("input[name=VesselAc]:checked").val(),
                VesselPc:$("input[name=VesselPc]:checked").val(),
                Cause:$("input[name=Cause]:checked").val(),
                CauseE:$("input[name=CauseE]:checked").val(),
                CauseLType:$("select#CauseLType").val(),
                CauseOther:$("input#CauseOther").val(),
                Stage:$("input[name=Stage]:checked").val(),
                stp_others:$("input#stp_others").val(),
                ProvisionalDiagnosis:$("input#ProvisionalDiagnosis").val(),                

                //Estimate Data
                estimate_i1:$("input#estimate_i1").is(':checked'),
                estimate_i2:$("input#estimate_i2").is(':checked'),
                estimate_i3:$("input#estimate_i3").is(':checked'),
                estimate_d1:$("input#estimate_d1").is(':checked'),
                estimate_d2:$("input#estimate_d2").is(':checked'),
                estimate_d3:$("input#estimate_d3").is(':checked'),
                estimate_d4:$("input#estimate_d4").is(':checked'),
                estimate_d5:$("input#estimate_d5").is(':checked'),
                estimate_d6:$("input#estimate_d6").is(':checked'),
                estimate_d7:$("input#estimate_d7").is(':checked'),
                estimate_d8:$("input#estimate_d8").is(':checked'),
                estimate_d9:$("input#estimate_d9").is(':checked'),
                estimate_d10:$("input#estimate_d10").is(':checked'),
                estimate_d11:$("input#estimate_d11").is(':checked'),
                estimate_d12:$("input#estimate_d12").is(':checked'),
                estimate_reason:$("textarea#estimate_reason").val(),

                //Management Data
                im_IVfluidYN:$("input#im_IVfluidYN").is(':checked'),
                im_O2CanulaYN:$("input#im_O2CanulaYN").is(':checked'),
                im_IntubationYN:$("input#im_IntubationYN").is(':checked'),
                im_OtherYn:$("input#im_OtherYn").is(':checked'),
                im_OtherTxt:$("#im_OtherTxt").val()
            },
            dataType: "html",
            success: function(data) {                
                $("#result").html("<input class='ui-button ui-widget ui-state-default ui-corner-all' type='submit' value='บันทึก'>&nbsp;&nbsp;&nbsp;");
                calStrokeTime();
            }
        });
        return false;
    });

    $( "#dialog:ui-dialog" ).dialog("destroy");
    
    $( "#dialog-confirm").dialog({
			resizable: false,
			height:200,
                        autoOpen: false,
			modal: true,
			buttons: {
				"ยืนยัน": function() {
                                    $("#frmConsultData").submit();
                                    $.ajax({
                                        type: "POST",
                                        url: "../ajax/endConsult",
                                        data: {
                                            c_id:$('input#c_id').val(),
                                            p_id:$('input#p_id').val()
                                        },
                                        dataType: "html",
                                        success: function(data){
                                            $( this ).dialog("close");
                                            parent.window.open("/TeleStroke/patient",'_self');
                                        }
                                    });
				},
				"ยกเลิก": function() {
                                    $( this ).dialog( "close" );
				}
			}
    });


    $('input#endConsultBtn').click(function() {
        $( "#dialog-confirm" ).dialog("open");
    });
});